package hot;

import java.util.ArrayList;
import java.util.List;

public class yijiujiu {
//    https://leetcode.cn/problems/binary-tree-right-side-view/submissions/676717190/?envType=study-plan-v2&envId=top-100-liked
    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
         this.right = right;
      }
 }
        static List<Integer> ret=new ArrayList<>();
        static int max_deepth=-1;
        public static List<Integer> rightSideView(TreeNode root) {
            dfs(root,0);
            return ret;
        }
        public static void dfs(TreeNode root,int currentdeepth){
            if(root==null){
                return;
            }
            if(currentdeepth>max_deepth){
                ret.add(root.val);
                max_deepth=currentdeepth;
            }
            dfs(root.right,currentdeepth+1);
            dfs(root.left,currentdeepth+1);

        }

        public static void main(String[] args) {
            TreeNode treeNode1 = new TreeNode(1);
            TreeNode treeNode2 = new TreeNode(2);
           TreeNode treeNode3 = new TreeNode(3);
            TreeNode treeNode4 = new TreeNode(4);
            TreeNode treeNode5 = new TreeNode(5);
            treeNode1.left = treeNode2;
            treeNode1.right = treeNode3;
            treeNode2.right = treeNode5;
            treeNode3.right=treeNode4;
            List<Integer> list = rightSideView(treeNode1);
            System.out.println(list);

        }

    }


